package collection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *set集合的演示
 */
public class SetDemo {
    public static void main(String[] args) {
        //小面试题:如何去重?
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        list.add("two");
        System.out.println("list:"+list);//[one, two, three, four, five, two]

        Set<String> set = new HashSet<>();
        set.addAll(list);
        System.out.println("set:"+set);//[four, one, two, three, five]

        /*Set<String> set = new HashSet<>();
        set.add("one");
        set.add("two");
        set.add("three");
        set.add("four");
        set.add("five");
        set.add("two");//无法正确添加进去，因为set无法添加相同元素
        System.out.println(set);
         */
    }
}
